492C - Vanya and Exams - CodeForces Solution


greedy sortings *1400

Please click on ads to support us..

Python Code:

x = [int(i) for i in input().split()]
n = x[0]
r = x[1]
goal = x[2] * n
exams = []

total = 0
for i in range(n):
  a, b = [int(i) for i in input().split()]
  total += a
  exams.append((b, a))

if total >= goal:
  print(0)
else:
  exams.sort()
  ans = 0   for exam in exams:
    avail = r - exam[1]
    if avail > 0:
      if avail > goal - total:
                ans += (goal-total) * exam[0]
        break
      else:
                ans += avail * exam[0]
        total += avail
  print(ans)


C++ Code:

#include <bits/stdc++.h>
#include<unordered_map>

using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) (int)v.size()
#define endl "\n"
typedef long long ll;
constexpr auto EPS = 1e-9;
const long long mod = (long long) 1e9 + 7;
const int N = 1e7 + 2;
const double pi = acos(-1);
const int oo = 0x3f3f3f3f;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

void Fast() {
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int main() {
    Fast();
    ll n, r, avg;
    cin >> n >> r >> avg;
    vector<pair<ll, ll>> grades(n);
    ll sum = 0;
    for (auto &it: grades) {
        cin >> it.second >> it.first;
        sum += it.second;
    }
    sort(all(grades));
    ll cnt = 0;
    ll need = avg * n;
    for (int i = 0; i < n; i++) {
        if (grades[i].second < r and sum < need) {
            if (r - grades[i].second < need - sum) {
                cnt += (r - grades[i].second) * grades[i].first;
                sum += r - grades[i].second;
            } else {
                cnt += (need - sum) * grades[i].first;
                sum += (need - sum);
            }
        }

    }

    cout << cnt << endl;
}


Comments

Submit
0 Comments
More Questions

36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections